
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox",
    //"text!/js/biz/keb/activity/definebt.html"
], function ($, IO, upui, up, jui, bootbox, definebt) {

    $('#fileupload').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs').src = '/images/loading.gif';

    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {
        //alert('Img'+youpaiImg);
        //alert(data._response.result.url);
        //document.getElementById('upimgs').src = data._response.result.url;
        //console.log(youpaiImg);
        //  console.log("fileuploaddone",data);
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';
        console.log("#upimgs" + alt);
        $("#upimgs" + alt).attr("src", data._response.result.url);
    });
    $(document).on("click",".publish",function(){

        var example_id = $(".title").attr("data-id");
        var title = $(".title").val();
        var course_name = $(".course option:selected").val();
        var course_id = $(".course option:selected").attr("data-id");
        var lecture_name = $(".lecture option:selected").val();
        var lecture_id = $(".lecture option:selected").val();
        var content = editor.html();
        var param = {example_id:example_id,title:title,content:content};

        if(title==""){
            alert("请输入案例名称");
            return;
        }
        var checks=$(".boxWrapper input:checked");
        if(checks.length==0)
        {
            alert("请选择课时");
            return ;
        }
        var lecture_ids=[];
        $.each(checks,function(i,ele){
            lecture_ids.push($(ele).val());
        });
        lecture_ids=lecture_ids.join(",");
        param.lecture_ids=lecture_ids;
        if(content==""){
            alert("请输入案例内容");
            return;
        }
        if(param&&param!='undefined'){
            bootbox.confirm("确定要修改该案例",function(result){
                if(result) {
                    IO.post("act/update_case",param,function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/pte/case_lb';
                            }
                        })
                    });
                }
            });
        }
    });

    $(".course").on("change",function(data){

        IO.post("/pte/api/getCourseLecture",{course_id:$(this).val()},function(d){
            var html="";
            for(var i=0;i< d.length;i++){
                html+='<input type="checkbox" value="'+d[i].lecture_id+'" />'+d[i].lecture_name+"&nbsp;&nbsp;&nbsp;"
            }
            // document.getElementsByClassName("lecture")[0].innerHTML=html;
            $(".boxWrapper").html(html);

            if(data.isTrigger){
                //第一次手动触发
                console.log('set1')
                setTimeout(function(){
                    var lecture_ids=$("#lecture_ids").val();
                    lecture_ids=lecture_ids.split(",");
                    for(var i=0;i<lecture_ids.length;i++)
                    {
                        console.log(lecture_ids[i])
                        $('input[value='+lecture_ids[i]+']').prop('checked',true)
                    }
                    },1000);
            }
        });

    })
    $(document).ready(function(){
        //var obj= document.getElementById("course");
        //if (obj.fireEvent)
        //{
        //    obj.fireEvent('onchange');
        //}
        //else
        //{
        //    obj.onchange();
        //}
        $("#course").trigger("change",1)
    })
});

